<?php
class Admin_Model_Comments extends Zend_Db_Table_Abstract
{
	/**
	 * Model Comment
	 * @author Nguyen Manh Hung
	 */
	
	protected $_name = TABLE_COMMENTS; 
	protected $_id = 'id';
	
	/**
	 * insert new comment to database
	 * @param int $gid
	 * @param int $uid
	 * @param text $comment
	 * @param int $active
	 * @author Nguyen Manh Hung
	 */
	public function insertComment($gid, $uid, $comment, $active = 1, $type = 0){
		$date = new Zend_Date(); 
		if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
                    $ipcilent=$_SERVER['HTTP_X_FORWARDED_FOR'];
                else $ipcilent=$_SERVER['REMOTE_ADDR'];
    	
		$row = $this->createRow();
		$row->gid = $gid;
		$row->uid = $uid;
		$row->comment = $comment;
		$row->date = $date->toString(Zend_Date::TIMESTAMP);
		$row->postip = $ipcilent;
		$row->active = $active;
                $row->type_comment = $type;
		$row->save();
		$id =$row->id;
		return $id;
	}
	
	public function updateComment($id, $comment, $active = 1){
		$db = Zend_Db_Table::getDefaultAdapter();
		$row = $this->find($id)->current();
		if($row){
			$row->comment = $comment;
			$row->active = $active;
			$row->save();
			return 1;
		}else{
			return 0;
		}
	}
	
	public function filterComment($filters = array(), $sortFeild = null, $sort = 'ASC', $limit = null){
		$select = $this->select();
		//add cac filter vao truy van tim kiem
		if(count($filters)>0)
		{
			foreach ($filters as $feild => $filter){
				$select->where($feild .' =?', $filter);
			}
		}
		//add sort vao truy van
		if(null != $sortFeild)
		{
			$select->order(array($sortFeild.' '.$sort));
		}
		
		//add limit - offset vao truy van		
		if(null != $limit)
		{			
			$select->limit($limit, 0);
		}
		return $this->fetchAll($select);
	}
}
